<input type="button" value="debug" id="i-debug"/>&nbsp;&nbsp;&nbsp;
<input type="button" value="session clean" id="i-session_clean"/>

<div id="i-warning-0" style="display: none;" class="warning_box">
</div>

<input id="i-uid" type="hidden" value="<?=$UID ?>" />

<div id="i-step1">
	<h2 id="i-step1-title">Шаг 1: Выбирете таблицу</h2>
	<select style="width: 200px;" name="table" id="i-main-table">
		<option value="">Не выбрано</option>
		<? foreach($tables as $table) :?>
		<option value="<?=$table?>"><?=$table?></option>
		<? endforeach; ?>
	</select>
	<br style="clear: both;" />
	<button id="i-step1-button">Далее</button>
</div>

<div style="margin-top: 25px;" id="i-step2">
</div>

<div style="margin-top: 25px;" id="i-step3">
</div>

<script type="text/javascript">
$(document).ready(function(){
	$('#i-step1-button').live('click', function(){
		$.get('/curd/pool_adding/',
			{
				'uid' : $('#i-uid').val(),
				'state' : 0,
				'table_name' : $('#i-main-table').val()
			},
			function (data) {
				console.log(data);
				var answer = $.parseJSON(data);
				if(answer.is_error == 1){
					if (answer.error_message == '_reload_') {
						window.location.reload();
						return;
					}
					$('#i-warning-0')
						.css('display','')
						.html(answer.error_message);
					return;
				}

				$('#i-warning-0').css('display','none');

				$('#i-main-table').attr('disabled','');
				$('#i-step1-button').attr('disabled','');
				$('#i-step1-title').css('color','#a3a3a3');

				$.get('/curd/meta_step2/?table=' + answer.table_name, function(data){
					$('#i-step2').html(data);
			});
		});
	});

	$('#i-step2-button').live('click',function(){

		$.get(
			'/curd/pool_adding/',
			{
				'uid' : $('#i-uid').val(),
				'state' : 1,
				'table_name' : $('#i-main-table').val(),
				'table_alias' : $('#i-table-alias').val(),
				'module_title' : $('#i-title').val(),
				'main_field' : $('#i-main-field').val(),
				'functions' : $('#i-functions').val() || [],
			},
			function (data) {
				console.log(data);
				var answer = $.parseJSON(data);
				if(answer.is_error == 1){
					if (answer.error_message == '_reload_') {
						window.location.reload();
						return;
					}
					$('#i-warning-1')
						.css('display','')
						.html(answer.error_message);
					return;
				}

				$('#i-warning-1').css('display','none');

				$('#i-title').attr('disabled','');

				$('#i-step2-title').css('color','#a3a3a3');
				$('#i-title-caption').css('color','#a3a3a3');

				$('#i-table-alias').attr('disabled','');
				$('#i-table-alias-caption').css('color','#a3a3a3');

				$('#i-main-field').attr('disabled','');
				$('#i-main-field-caption').css('color','#a3a3a3');

				$('#i-functions').attr('disabled','');
				$('#i-functions-caption').css('color', '#a3a3a3');

				$('#i-step2-button').attr('disabled','');

				$.get('/curd/meta_step3/?table=' + answer.table_name, function(data){
					$('#i-step3').html(data);
					$(".ajax").click(function(){
						$.get($(this).attr('href'), function(HTML){
							try{
								json = $.parseJSON(HTML);
								console.log(json);
								if(json.is_error == 1){
									if (answer.error_message == '_reload_') {
										window.location.reload();
										return;
									}

									$('#i-warning')
										.css('display','')
										.html(json.error_message);
									return;
								}
							}catch(e){}
							$.fancybox(HTML,{autoDimensions:false});
						});
						return false;
					});
				});
			}
		);
	});

	$('#i-step3-button').live('click', function(){
		console.log('step 3 start sync');
		$.get('/curd/commit_adding',{'uid' : $('#i-uid').val()}, function (data) {
			var answer = $.parseJSON(data);
			if(answer.is_error == 1){
				if (answer.error_message == '_reload_') {
					window.location.reload();
					return;
				}
				$('#i-warning-commit')
					.css('display','')
					.html(answer.error_message);
				return;
			} else {
				if (answer.command == 'return to home') {
					window.location.href = '/curd';
				}
			}
		});
	});

	$('#i-debug').click(function(){
		$.get('/curd/debug/', function(data){
			console.log(data);
		});
	});

	$('#i-session_clean').click(function(){
		$.get('/curd/session_clean/', function(data){
			console.log(data);
		});
	});
});
</script>